﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Data.SqlClient;

namespace HotelManagement
{
    public partial class RoomsFRM : Form
    {
        SqlConnection Cnn = new SqlConnection(Globals.DBConnection());
        DataSet ds = new DataSet();
        DataSet ds2 = new DataSet();  ///diversa
        SqlDataAdapter da;
        SqlDataAdapter da2;  ///
        int rm_id;
        public RoomsFRM()
        {
            InitializeComponent();
        }

        private void button2_Click(object sender, EventArgs e)
        {
            RoomsAttributes rmsatt = new RoomsAttributes();
            rmsatt.ShowDialog();

            da2 = new SqlDataAdapter("select rt_type_name,rt_id from Room_Type", Cnn);
            ds.Clear();
            da2.Fill(ds2);
            comboBox1.DataSource = ds2.Tables[0];
            comboBox1.DisplayMember = "rt_type_name";
            comboBox1.ValueMember = "rt_id";
        }

        private void RoomsFRM_Load(object sender, EventArgs e)
        {
            // TODO: This line of code loads data into the 'hotelManagementSystemDataSet.Room_Type' table. You can move, or remove it, as needed.
            //this.room_TypeTableAdapter.Fill(this.hotelManagementSystemDataSet.Room_Type);

            da = new SqlDataAdapter("select rm_id as [ردیف], rm_number as [شماره اتاق],rm_floor as [طبقه],rt_type_name as [نوع اتاق] from Room,Room_Type where rm_type=rt_id", Cnn);
            da.Fill(ds);
            DgViewRoom.DataSource = ds.Tables[0];
            da2 = new SqlDataAdapter("select rt_type_name,rt_id from Room_Type", Cnn);
            da2.Fill(ds2);
            comboBox1.DataSource = ds2.Tables[0];
            comboBox1.DisplayMember = "rt_type_name";
        }

        private void SaveRoomDataBtn_Click(object sender, EventArgs e)
        {
            if(!string.IsNullOrEmpty(RoomNumberTxt.Text)&&!string.IsNullOrEmpty(RoomFlorTxt.Text)&&!string.IsNullOrEmpty(comboBox1.Text))
            {
            SqlCommand SaveINDB = new SqlCommand("INSERT INTO Room (rm_number,rm_floor,rm_type) values (@rm_number,@rm_floor,@rm_type)", Cnn);
            SaveINDB.Parameters.AddWithValue("@rm_number", RoomNumberTxt.Text.Trim());
            SaveINDB.Parameters.AddWithValue("@rm_floor", int.Parse(RoomFlorTxt.Text.Trim()));
            SaveINDB.Parameters.AddWithValue("@rm_type", (int)comboBox1.SelectedValue);

            Cnn.Open();
            SaveINDB.ExecuteNonQuery();
            Cnn.Close();

            ds.Clear();
            da.Fill(ds);
            DgViewRoom.DataSource = ds.Tables[0];
            }
            else
            {
                MessageBox.Show("لطفا همه قسمت ها را به درستی تکمیل نمایید");
            }
        }

        private void DeleteRoomDataBtn_Click(object sender, EventArgs e)
        {
            if (MessageBox.Show("آیا برای حذف اتاق مطمئنید؟", "Delete", MessageBoxButtons.YesNo, MessageBoxIcon.Question) == DialogResult.Yes)
            {
                SqlCommand DeleteCMD = new SqlCommand("DELETE FROM Room WHERE rm_id=@rm_id", Cnn);
                DeleteCMD.Parameters.AddWithValue("@rm_id", rm_id);
                Cnn.Open();
                try
                {
                    DeleteCMD.ExecuteNonQuery();
                }
                catch (SqlException)
                {
                    MessageBox.Show("این اتاق در حال استفاده است", "خطا", MessageBoxButtons.OK, MessageBoxIcon.Error);
                    Cnn.Close();
                    return;
                }
                finally
                {
                    Cnn.Close();
                }

                ds.Clear();
                da.Fill(ds);
                DgViewRoom.DataSource = ds.Tables[0];

                DeleteRoomDataBtn.Enabled = false;
                MessageBox.Show("حذف اتاق با موفقیت انجام شد");

            }
        }

        private void DgViewRoom_CellMouseDoubleClick(object sender, DataGridViewCellMouseEventArgs e)
        {
            rm_id = (int)DgViewRoom.CurrentRow.Cells[0].Value;
            DeleteRoomDataBtn.Enabled = true;
        }
    }
}
